package phonecompany.phone;

import java.io.Serializable;

import lint4j.f;

/**
 * This class defines the Phone number construct.
 * 
 * @since Oct 24, 2011
 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
 * @version $Id: PhoneNumber.java 28 2011-10-30 21:25:58Z zexpp5@gmail.com $
 * 
 */
public final class PhoneNumber implements Serializable {
	private AreaCode areaCode;
	private String localNumber;

	/**
	 * To construct a phone number by area code and local number.
	 * 
	 * Constructor Method
	 * 
	 * @param areaCode
	 * @param localNumber
	 */
	public PhoneNumber(int areaCode, int localNumber) {
		this.areaCode = AreaCode.area(areaCode);
		this.localNumber = String.valueOf(localNumber);
	}

	/**
	 * * To construct a phone number by full phone number.
	 * 
	 * Constructor Method
	 * 
	 * @param fullNumber
	 */
	public PhoneNumber(String fullNumber) {
		this.areaCode = AreaCode.area(Integer.parseInt(fullNumber.substring(0,
				4)));
		this.localNumber = fullNumber.substring(4, 10);
	}

	private PhoneNumber() {

	}

	/**
	 * Overrided toString() method which will return the full phone number
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		
		return String.valueOf(areaCode.areaCode()) + localNumber;
	}

	/**
	 * overrided equals() method return true when phoneNUmbers have same name
	 * and age.
	 * 
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this.hashCode() == obj.hashCode()) {
			return true;
		}
		return false;
	}

	/**
	 * To correspond the overrided equals method.
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		int hc = 17;
		hc = 37 * hc
				+ (this.toString() == null ? 0 : this.toString().hashCode());
		return hc;
	}
}
